// my_other/jybx/jybx.js
let app = getApp()
let yuming = app.globalData.yuming
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '经营保险',
    page: 1,
    gongwu: false,
    ispaybox: false,
    bxprice: ''
    // bxlist: [{
    //     id: 1,
    //     title: '安心百分百加百万随我行',
    //     jie: '加入食品安全险百万保障，经营更无忧',
    //     baoz: '百万保障',
    //     price: '250.00',
    //     isBuy: 0
    //   },
    //   {
    //     id: 2,
    //     title: '安心百分百加百万随我行',
    //     jie: '加入食品安全险百万保障，经营更无忧',
    //     baoz: '百万保障',
    //     price: '250.00',
    //     isBuy: 1
    //   }
    // ]
  },
  // 查看更多
  lookmore() {
    shuzong = this.data.bxlist
    this.setData({
      page: (this.data.page) + 1
    })
    this.getbxList()
  },
  getbxList() {
    let that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'shop/shop_apply_baoxian_list', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page
    }, 'POST').then(res => {
      wx.hideLoading();
      if (res.data.code == 200) {
        shu = res.data.data
        if (that.data.page != 1) {
          if (shu.length != 10) {
            wx.showToast({
              title: '没有更多数据了~',
              icon: "none"
            })
            that.setData({
              gongwu: true
            })
          }
        }
        this.setData({
          bxlist: shuzong.concat(shu)
        })
      }
    })
  },
  xq(e) {
    let id = e.currentTarget.dataset.id;
    let title = e.currentTarget.dataset.title;
    wx.navigateTo({
      url: '/my_other/jybxxq/jybxxq?id=' + id + '&title=' + title,
    })
  },
  tishi(e) {
    let id = e.currentTarget.dataset.id;
    let price = e.currentTarget.dataset.price;
    let that = this;
    wx.showModal({
      title: '您确定要购买吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          console.log("确定")
          that.buy(id, price)
        } else {
          console.log("取消")
        }
      }
    })
  },
  buy(id, price) {
    this.setData({
      bxprice: price
    })
    this.myRequest(yuming + 'shop/shop_apply_baoxian', {
      shopId: wx.getStorageSync('shopId'),
      bxId: id,
      price
    }, 'POST').then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        let orderSn = res.data.data.orderSn
        this.setData({
          ispaybox: true,
        })
        setTimeout(() => {
          this.setData({
            ispaybox: false
          })
          this.monibuy(orderSn);
        }, 3000)
      }
    })
  },
  //模拟支付
  monibuy(orderSn) {
    setTimeout(() => {
      this.myRequest(yuming + 'Notify/pay_notify', {
        out_trade_no: orderSn
      }, 'POST').then(res => {
        wx.hideLoading()
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        if (res.data.code == 200) {
          this.getbxList();
        }
      })
    }, 1000)
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
    // wx.switchTab({
    //   url: '/pages/index/index',
    // })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getbxList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})